package com.thinkgem.jeesite.common.utils;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

import java.io.*;

/**
 * 用来解密配置中的密文(重点配置，在这里扩展用户名的解密)
 * setUsername(name) 方法对应xml中的一个property属性，password默认加密不需要重写，
 * 还可以加密url 重写setUrl(url)
 */
@SuppressWarnings("all")
public class DecryptDruidSource extends DruidDataSource{
    /** MySQL安装目录的Bin目录的绝对路径 */
    private String mysqlBinPath;

    @Override
    public void setUsername(String username) {
        /*try {
            username = ConfigTools.decrypt(username);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        super.setUsername(username);
    }

    public static void main(String[] args) throws Exception{
        System.out.println(ConfigTools.encrypt("Yousmile@123"));
    }

    /**
     * 备份数据库
     *
     * @param output 输出流
     * @param dbname 要备份的数据库名
     */
    public String backup(){
        mysqlBinPath = DictUtils.getDictValue("db", "path", "C:\\\"Program Files (x86)\"\\xinhuasd") + "\\MySQL\\\"MySQL Server 5.6\"\\bin\\";
        String filepath = DictUtils.getDictValue("db", "bkpath", "d:") + "//bookmgr-backup-" + DateUtils.getDate() + ".sql";

        try {
            Process process = Runtime.getRuntime().exec("cmd /c " + mysqlBinPath + "mysqldump -u" + username + " -p" + password + " --set-charset=utf8 bookmgr --result-file=" + filepath);
            InputStream ins = process.getInputStream();
            byte[] b = new byte[8192];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while((i = ins.read(b)) != -1){
                sb.append(new String(b), 0, i);
            }
            ins.close();
            return filepath;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 恢复数据库
     *
     * @param input 输入流
     * @param dbname 数据库名
     */
    public void restore(InputStream input){
        mysqlBinPath = DictUtils.getDictValue("db","path","");
        String command = "cmd /c " + mysqlBinPath + "mysql -u" + username + " -p" + password + " --default-character-set=utf8 bookmgr ";
        try {

            InputStreamReader inputReader = new InputStreamReader(new FileInputStream("D:\\bk_action_log.sql"), "utf-8");
            BufferedReader br = new BufferedReader(inputReader);

            Process process = Runtime.getRuntime().exec(command);
            OutputStream out = process.getOutputStream();
            String str = null;
            StringBuffer sb = new StringBuffer("");
            while ((str = br.readLine()) != null) {
                sb.append(str + "/r/n");
            }
            str = sb.toString();
            OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
            writer.write(str);
            writer.flush();
            out.close();
            br.close();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}